import { loader, Prefab, instantiate } from "cc";

class ResInfo {
    path = "";     //资源路径
    type = null;   //资源类型
    uuids = new Set();  //使用的资源id
    obj = null;    //资源对象
    ref = 0;       //引用计数
}

window.ResMgr = {
    resPool: new Map(),
    uuidPool: new Map(),

    //加载资源
    LoadRes: function(path, type, callback) {
        if (this.resPool.has(path)) {
            console.error("loadRes: "+path+" err:already exist");
            if (callback!=null) {
                callback("err:already exist");
            }
        }
        loader.loadRes(path, type, (err, prefab)=> {
            if (err==null) {
                let newInfo = new ResInfo();
                newInfo.path = path;
                newInfo.obj = prefab;
                newInfo.type = type;
                //记录所有相关资源
                for (let key of loader.getDependsRecursively(prefab)) {
                    newInfo.uuids.add(key);
                    let u = this.uuidPool.get(key);
                    if (u) {
                        this.uuidPool.set(key,u+1);
                    } else {
                        this.uuidPool.set(key,1);
                    }
                }
                this.resPool.set(path, newInfo);
            }
            console.log("loadRes: "+path+" err:"+err);
            if (callback!=null) {
                callback(err);
            }
        });
    },
    //获取实例
    GetInstance: function(path) {
        let p = this.resPool.get(path);
        if(p!=null){
            p.ref++;
            if (p.type==Prefab) {
                return instantiate(p.obj);
            }
            else{
                return p.obj;
            }
        }
        return null;
    },
    GetInstanceArrary: function(path, num) {
        let arr = [];
        let p = this.resPool.get(path);
        if(p!=null && num>0){
            for (let index = 0; index < num; index++) {
                if (p.type==Prefab) {
                    arr.push(instantiate(p.obj));
                }
                else{
                    arr.push(p.obj);
                }
            }
            p.ref+=num;
        }
        return arr;
    },
    //释放实例
    ReleaseInstance: function(path, num=1) {
        let p = this.resPool.get(path);
        if(p!=null){
            p.ref -= num;
            //释放资源
            if (p.ref<=0) {
                this._ReleaseRes(p);
            }
        }
    },
    //释放资源
    _ReleaseRes: function(p) {
        p.uuids.forEach((key)=>{
            let u = this.uuidPool.get(key);
            console.log(key+": "+u);
            if (u>1) {
                this.uuidPool.set(key,u-1);
                p.uuids.delete(key);
            } else {
                this.uuidPool.delete(key);
            }
        });
        let arr = Array.from(p.uuids);
        loader.release(arr);
        loader.releaseRes(p.path);
        this.resPool.delete(p.path);
    },
    //加载并创建实例，用于Prefab
    LoadPrefab: function(path, auto, callback) {
        let p = this.GetInstance(path);
        if(p){
            if (auto) {
                p.addComponent("AutoRelease").resUrl = path;//可以销毁节点时自动释放资源
            }
            callback(null, p);
        } else {
            this.LoadRes(path, Prefab, err=> {
                if (err) {
                    callback(err, null);
                } else {
                    p = this.GetInstance(path);
                    if (auto) {
                        p.addComponent("AutoRelease").resUrl = path;
                    }
                    callback(null, p);
                }
            });
        }
    }
}